'use strict';
module.exports = {
	/**
	 * 微信支付宝完成修改状态
	 * @url client/order/kh/payChange 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, order_id } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------

		await vk.baseDao.updateById({
			dbName: "uni-id-base-order",
			id: order_id,
			dataJson: {
				success_year: new Date().getFullYear(),
				success_month: new Date().getMonth() + 1,
				success_day: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd"),
				success_week: new Date().getDay(),
			}
		})

		let good = await vk.baseDao.findByWhereJson({
			dbName: "order_goods",
			whereJson: { oid: order_id }
		})
		let course = await vk.baseDao.findById({
			dbName: "course",
			id: good.goods_id
		});
		let order = await vk.baseDao.findById({
			dbName: "uni-id-base-order",
			id: order_id
		})
		await vk.baseDao.update({
			dbName: "user_goods",
			whereJson: {
				goods_id: course._id,
				user_id: uid
			},
			dataJson: {
				end_time: order.create_time + course.validity_period * 24 * 60 * 60 * 1000
			}
		})

		let info = await vk.baseDao.findById({
			dbName: "uni-id-users",
			id: uid
		})
		await UniPush("toSingle", {
			uid,
			"type": 'buy',
			"title": "成功购买提醒",
			"content": "您已成功购买课程",
			"payload": JSON.stringify({
				"title": "成功购买提醒",
				"content": "您已成功购买课程",
				"type": 'buy',
				"isUser": true
			}),
			"clientid": info.clientid
		})

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
